<template>
  <div class="collections" ref="warrper">
    <div class="collections-contant">
      <div class="box-contant" v-for="(item,index) in collectionsList" :key="index">
        <div class="img-contant">
          <i class="iconfont icon-shoucangdedian icon-dizhi"></i>
        </div>
        <div class="text-contant">
          <div class="address">{{item.title}}</div>
          <div class="des">{{item.address}}</div>
        </div>
        <div class="star-contant" @click="clickCancel(item.id)">
          <i class="iconfont icon-shoucangdedian"></i>
        </div>
      </div>
    </div>
    <div class="notdata" v-if="collectionsList.length<=0">
      <img :src="nothing">
      <div class="text">暂无信息</div>
    </div>
  </div>
</template>

<style lang="less" scoped>
@import "index.less";
</style>

<script>
import { mapActions } from "vuex";
import BScroll from "better-scroll";
export default {
  name: "collections",
  data() {
    return {
      // 数据
      collectionsList: {},
      // 滑块
      listScroll: {},
      page: 1, // 翻页页数
      // 安全锁
      lock: false,
      nothing: require("../../assets/nothing.png")
    };
  },
  created() {
    this.change(false);
    // 获取显示列表
    this.getList();
  },
  methods: {
    ...mapActions("Home", ["change"]),
    // 获取显示的收藏
    getList() {
      this.$dialog.loading.open("正在加载");
      this.$http
        .get("/device/Oftenlist", {
          params: {
            page: 1
          }
        })
        .then(res => {
          this.collectionsList = res.data.data;
          this.scrollInit();
          this.$dialog.loading.close();
        });
    },
    scrollInit() {
      this.listScroll = new BScroll(this.$refs.warrper, {
        click: true,
        pullUpLoad: {
          threshold: -50
        }
      });
      this.listScroll.on("pullingUp", () => {
        if (this.lock) {
          return;
        }
        this.$dialog.loading.open("正在加载");
        // 页数加一
        this.page++;
        // 锁定防止多次加载
        this.lock = true;
        this.$http
          .get("/device/Oftenlist", {
            params: {
              page: this.page
            }
          })
          .then(res => {
            // 请求回来的数据相加
            this.collectionsList = this.collectionsList.concat(res.data.data);
            if (res.data.data.length <= 0) {
              this.$dialog.toast({
                mes: "没有更多的数据",
                timeout: 1500,
                icon: "none"
              });
              this.lock = true;
            } else {
              // 解锁上拉
              this.lock = false;
            }
            this.$forceUpdate();
            this.listScroll.finishPullUp();
            this.listScroll.refresh();
            this.$dialog.loading.close();
          });
      });
    },
    jumpTo(ID) {
      this.$router.push({
        path: "/Stations",
        query: {
          id: ID
        }
      });
    },
    clickCancel(id) {
      this.$dialog.loading.open("正在加载");
      this.$http
        .post("/del/Deviceoften", {
          id: id
        })
        .then(res => {
          if (res.data.errno == 0) {
            this.$dialog.toast({
              mes: "取消收藏成功",
              timeout: 1500,
              icon: "success"
            });
            this.getList();
          } else {
            this.$dialog.toast({
              mes: "取消收藏成功",
              timeout: 1500,
              icon: "error"
            });
          }
          this.$dialog.loading.close();
        });
    }
  },
  components: {}
};
</script>
